package com.his.login.service.Imp;

import com.his.login.dao.DictEmployeeMapper;
import com.his.login.entity.DictEmployee;
import com.his.login.service.LoginService;
import com.his.login.vo.EmployeeVo;
import com.his.login.vo.LoginVo;
import com.his.login.vo.UpdatePasswordVo;
import com.xiao.common.exception.HisExecption;
import com.xiao.common.service.SessionService;
import com.xiao.common.util.BeanUtil;
import com.xiao.common.util.MD5Util;
import com.xiao.common.util.RedisUtils;
import com.xiao.common.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author xiaoxiaoping
 * @Date 2020/2/3 11:26
 * @Version 1.0
 */
@Service
public class LoginServiceImpl implements LoginService {
    @Resource
    private UUIDUtil uuidUtil;
    @Resource
    private RedisUtils redisUtils;
    @Resource
    private DictEmployeeMapper dictEmployeeMapper;
    @Resource
    private SessionService sessionService;

    @Override
    public String doLogin(@Validated LoginVo loginVo) {
        DictEmployee dictEmployee = BeanUtil.copyBean(loginVo,DictEmployee.class);
        dictEmployee.setEMPPASSWORD(MD5Util.MD5Encode(dictEmployee.getEMPPASSWORD(),"utf-8"));
        dictEmployee.setSTOPFLAG(0);
        List<DictEmployee> list = dictEmployeeMapper.selectByEntity(dictEmployee);
        if(list.size()>0){
            //生成toke
            String token = uuidUtil.uuid();
            redisUtils.set(token,list.get(0),10800);
            return token;
        }
        return null;
    }

    @Override
    public String isLogin(String token) {
        Boolean status = redisUtils.hasKey(token);
        if(status){
            return token;
        }else{
            return null;
        }
    }

    @Override
    public void updatePassword(UpdatePasswordVo updatePasswordVo,String token) {
        DictEmployee dictEmployee = (DictEmployee) sessionService.getSessionUser(token);
        if(null == dictEmployee){
            throw  HisExecption.serverErrException( "账号已退出请登录！");
        }
        if(! updatePasswordVo.getNEWEMPPASSWORD().equals(updatePasswordVo.getCONFIRMPASSWORD())){
            throw  HisExecption.serverErrException("新密码与确认密码不一致");
        }
        String oldPassword = MD5Util.MD5Encode(updatePasswordVo.getOLDEMPPASSWORD(),"utf-8");
        if(oldPassword.equals(dictEmployee.getEMPPASSWORD())){
            DictEmployee dictEmployeeNew = new DictEmployee();
            dictEmployeeNew.setEMPID(dictEmployee.getEMPID());
            dictEmployeeNew.setEMPPASSWORD(MD5Util.MD5Encode(updatePasswordVo.getNEWEMPPASSWORD(),"utf-8"));
            dictEmployeeMapper.updateById(dictEmployeeNew);
            redisUtils.del(token);
        }else{
            throw  HisExecption.serverErrException( "原密码错误！");
        }

    }

    @Override
    public void loginOut(String token) {
        redisUtils.del(token);
    }
}
